package com.sgq.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sgq.entity.Department;
import com.sgq.service.DepartmentService;
import com.sgq.utils.DepartmentTree;
import com.sgq.utils.Result;
import com.sgq.vo.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sgq
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/api/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/{id}")
    public Result getDepartmentById(@PathVariable("id") Long id){
        return Result.ok(departmentService.getById(id));
    }

    @GetMapping("/list")
    public Result list(DepartmentQueryVo departmentQueryVo){

        List<Department> departmentList = departmentService.findDepartmentList(departmentQueryVo);
        //找到最大的父级部门id =》 找到顶级部门的id
        if (departmentList.size() > 0){
            // List<Long>
            //获取所有的父级部门id =>
            Long maxParentId = departmentList.stream().map(Department::getPid).min(Long::compareTo).get();
            List<Department> departments = DepartmentTree.makeDepartmentTree(departmentList, maxParentId);
            return Result.ok(departments);
        }
        //按条件没有找到数据，则返回长度为0的列表
        List<Department> departments = DepartmentTree.makeDepartmentTree(departmentList, -100L);
        return Result.ok(departments);
    }

    @GetMapping("/getParentTreeList")
    public Result getParentTreeList(){
        return Result.ok(departmentService.findParentDepartment());
    }

    @GetMapping("/page")
    public Result page(DepartmentQueryVo departmentQueryVo){
        IPage<Department> page = departmentService.findPage(departmentQueryVo);
        return Result.ok(page);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Department department){
        //TO DO: 密码加密
        return Result.ok(departmentService.save(department));
    }

    @PutMapping("/update")
    public Result update(@RequestBody Department department){
        return Result.ok(departmentService.updateById(department));
    }

    /**
     *  核查是否存在子部门
     * @param id 部门id
     * @return
     */
    @GetMapping ("/check/{id}")
    public Result check(@PathVariable("id") Long id){
        //核对是否存在子部门
        boolean check = departmentService.check(id);
        // 核对是否存在用户
        boolean b = departmentService.hasUserInDepartment(id);

        return check || b == true ?  Result.exist().message("该部门存在子部门或已分配用户") :  Result.ok();

    }

    @DeleteMapping("/delete/{id}")
    public Result update(@PathVariable("id") Long id){
        return Result.ok(departmentService.removeById(id));
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody Long [] ids){
        return Result.ok(departmentService.removeBatchByIds(Arrays.asList(ids)));
    }


}

